package com.example.web.controller;


import com.example.demodao.entity.TbCart;
import com.example.service.AbstractCart;
import com.example.service.TbItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author rosszhang
 * @since 2023-01-05
 */
@Slf4j
@RestController
@RequestMapping("/cartInfo")
public class TbCartController {
    @Autowired
    private TbItemService tbItemService;
    @Autowired
    private ApplicationContext applicationContext;

    @GetMapping("right")
    public TbCart right(@RequestParam("userId") int userId) {
        Map<Long, Integer> items = new HashMap<>();
        String userCategory = getUserCategory(userId);
        AbstractCart cart = (AbstractCart) applicationContext.getBean(userCategory + "UserCart");
        log.info("获取到的getBean：{}",applicationContext.getBean(userCategory + "UserCart"));
        return cart.process(userId, items);
    }

    private static String getUserCategory(int userId) {
        if (userId == 1) {
            return "Vip";
        }
        if (userId == 2) {
            return "Internal";
        }
        return "Normal";
    }

}
